package us.icitap.cm.ui.policecase;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.entities.AppEntity;
import us.icitap.cm.business.entities.Case;
import us.icitap.cm.business.entities.Report;
import us.icitap.cm.business.entities.ReportTypeEnum;
import us.icitap.cm.listeners.ReturnObjectListener;
import us.icitap.cm.ui.shared.LoadCaseWindow;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.server.Sizeable.Unit;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Tree;
import com.vaadin.ui.VerticalLayout;

public class DisplayCaseLayout extends VerticalLayout{

	
	private Case pCase;

	Tree caseTree = new Tree();
	final HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
	
	
	public DisplayCaseLayout() {
		final LoadCaseWindow loadCaseWindow = new LoadCaseWindow(false);
		loadCaseWindow.addListener(new ReturnObjectListener() {
			
			@Override
			public <T> void returnObject(T result) {
				if(result != null){
					loadUI();
					loadCase((Case) result);
					loadCaseWindow.close();
				}
			}
		});
		
		CaseManagementUI.getCurrent().addWindow(loadCaseWindow);
	}
	
	private void loadCase(Case _case){
		this.pCase = _case;
		this.populateTree();
		
//		ViewCaseLayout vcLayout = new ViewCaseLayout();
//		this.addComponent(vcLayout);
	}
	
	private void loadUI(){
		
		this.addComponent(splitPanel);
		
		VerticalLayout vl = new VerticalLayout();
		vl.addComponent(caseTree);
		
		splitPanel.setFirstComponent(vl);
		
		splitPanel.setHeight((CaseManagementUI.getCurrent().getPage().getBrowserWindowHeight() - 184) + "px");
		splitPanel.setMinSplitPosition(200, Unit.PIXELS);
		splitPanel.setMaxSplitPosition(200, Unit.PIXELS);
		
		caseTree.setImmediate(true);
		
		caseTree.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				if(event.getProperty() != null){
					
					if (event.getProperty().getValue() instanceof VerticalLayout){
							DisplayCaseLayout.this.splitPanel.setSecondComponent((Component) event.getProperty().getValue());
							System.out.println(CaseManagementUI.getCurrent().getPage().getBrowserWindowHeight());
					}
				}	
			}
		});

		
	}
	
	private void populateTree(){
		caseTree.addItem("CASE");
		
		for (Report r : pCase.getReportList()) {
			VerticalLayout vl;
			if(r.getReportType().equals(ReportTypeEnum.ER)){
				vl = ViewCaseLayout.showEventReport(r); 
				
				caseTree.addItem(vl);
				caseTree.setItemCaption(vl, r.toString());
				caseTree.setParent(vl, "CASE");
			}
			else if(r.getReportType().equals(ReportTypeEnum.SR)){
				vl = ViewCaseLayout.showSupplementReport(r);
				caseTree.addItem(vl);
				caseTree.setItemCaption(vl, r.toString());
				caseTree.setParent(vl, "CASE");
			}
			
		}
		caseTree.expandItemsRecursively("CASE");
	}
	
}
